package collection;

import java.util.*;

/**
 * 表格数据的存储
 */
public class TestStoreDate {
    public static void main(String[] args) {
        /**先使用map,再使用List套起来**/
        Map<String,Object> row1 = new HashMap<>();
        row1.put("id",1001);
        row1.put("name","大王");
        row1.put("salary",200);

        Map<String,Object> row2 = new HashMap<>();
        row2.put("id",1002);
        row2.put("name","大刘");
        row2.put("salary",100);

        List<Map<String,Object>> table1 = new ArrayList<>();
        table1.add(row1);
        table1.add(row2);
        for (Map<String,Object> row:table1){
            Set<String> keySet = row.keySet();
            for (String key:keySet){
                System.out.print(key+":"+row.get(key)+"\t");
            }
            System.out.println();
        }
        System.out.println(table1.toString());


        /**每一行使用javabean对象,再使用List/Map套起来**/

        User user1 = new User(1003,"张三",500);
        User user2 = new User(1004,"王五",80);
        /**存到List里**/
       List<User> table2 = new ArrayList<>();
       table2.add(user1);
       table2.add(user2);

       for (User data:table2){
          System.out.println(data);
       }

       /**存到Map里**/
       Map<Integer,User> table3 = new HashMap<>();
       table3.put(1,user1);
       table3.put(2,user2);

       Set<Integer> itg = table3.keySet();
       for (Integer u:itg){
            System.out.println(u+"-------"+table3.get(u));
       }
    }
}

class User{
    /**完整的一个javabean要有set/get方法。一个无参构造器**/
    private int id;
    private String name;
    private int salary;

    public User(){

    }

    public User(int id, String name, int salary) {
        this.id = id;
        this.name = name;
        this.salary = salary;
    }

    public int getId() {
        return id;
    }

    @Override
    public String toString() {
        return "id:"+id+",name:"+name+",salary:"+salary;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getSalary() {
        return salary;
    }

    public void setSalary(int salary) {
        this.salary = salary;
    }
}
